<template>
	<div class="container">
		<!-- 路径导航区域 -->
		<el-breadcrumb separator="/">
			<el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
			<el-breadcrumb-item>课程管理</el-breadcrumb-item>
			<el-breadcrumb-item>课程管理</el-breadcrumb-item>
		</el-breadcrumb>
		<!-- 搜索区域 -->
		<el-row>
			<el-form :inline="true">
				<el-form-item label="课程名称">
					<el-input v-model="queryInfo.courseName" @clear="queryList" placeholder="请输
入课程名称"></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" @click="queryList">搜索</el-button>
					<el-button type="primary" @click="toAddcourse">新增课程</el-button>
					<!-- <el-button type="danger">批量删除</el-button> -->
				</el-form-item>
			</el-form>
		</el-row>
		<!-- 表格区域 -->
		<el-table :data="courseList" border style="width: 100%">
			<el-table-column type="selection" width="55"></el-table-column>
			<el-table-column prop="courseId" label="ID"></el-table-column>
			<el-table-column prop="courseName" label="课程名称">
			</el-table-column>
			<el-table-column prop="consultId" label="讲师">
				<template slot-scope="scope">
					{{consults[scope.row.consultId]}}
				</template>
			</el-table-column>
			<el-table-column prop="courseTypeId" label="课程分类">
				<template slot-scope="scope">
					{{courseTypes[scope.row.courseTypeId]}}
				</template>
			</el-table-column>
			<el-table-column prop="imgs" label="课程图片">
				<template slot-scope='scope'>
					<el-image style="width: 100px;
					height: 100px" :src="url+'download?filename='+scope.row.imgs"></el-image>
				</template>
			</el-table-column>
			<el-table-column prop="courseIntroduce" label="课程简介">
			</el-table-column>
			<el-table-column prop="courseTime" label="课程时长">
			</el-table-column>
			<!-- <el-table-column prop="coursePicture" label="课程图片">
				<template slot-scope='scope'>
					<el-image style="width: 100px;
					height: 100px" :src="url+'download?filename='+scope.row.coursePicture"></el-image>
				</template>
			</el-table-column> -->
			<el-table-column prop="courseCount" label="课程节数">
			</el-table-column>
			<el-table-column prop="people" label="适用人群">
			</el-table-column>
			<!-- <el-table-column prop="readingVolume" label="阅读量">
			</el-table-column>
			<el-table-column prop="collectionVolume" label="收藏量">
			</el-table-column>
			<el-table-column prop="reviewsCount" label="评价量">
			</el-table-column> -->
			
			<el-table-column prop="videoPath" label="查看视频">
				<template slot-scope='scope'>
					<el-link type="primary" @click="toSeeVideo(scope.row)">查看视频</el-link>
					<el-dialog
						@close="cancel"
					  title="当前视频"
					  :visible.sync="dialogVisible"
					  width="30%"
					  :before-close="handleClose">
					  <div v-if="videoUrl != ''">
						  <video :src="url+'download?filename='+videoUrl" style="width: 400px; height: 300px;" controls="controls"></video>
					  </div>
					  <el-result v-if="!videoUrl" icon="error" title="错误提示" subTitle="当前课程没有添加视频">
					  </el-result>
					  <span slot="footer" class="dialog-footer">  
					    <el-button @click="dialogVisible = false">取 消</el-button>
					    <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
					  </span>
					  
					</el-dialog>
				</template>
			</el-table-column>


			<el-table-column label="操作">
				<template slot-scope='scope'>
					<el-link type="primary" @click="toEditcourse(scope.row)">编辑课程</el-link>
					<el-link type="primary" @click="toDeletecourse(scope.row.courseId)">删除课程</el-link>
				</template>
			</el-table-column>
		</el-table>
		<!-- 分页区域 -->
		<el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
			:current-page="queryInfo.pageNum" :page-sizes="[5, 10, 15, 20]" :page-size="queryInfo.pageSize"
			layout="total, sizes, prev, pager, next, jumper" :total="total">
		</el-pagination>


		<!-- 新增和修改的对话框 -->
		<el-dialog :title="title" :visible.sync="dialogFormVisible" @close="cancel">
			<el-form :model="course">
				<el-form-item label="课程名称" label-width="80px">
					<el-input placeholder="请输入课程名称" v-model="course.courseName"></el-input>
				</el-form-item>
				<el-form-item label="讲师" label-width="80px">
					<!-- <el-input placeholder="请输入讲师" v-model="course.consultId"></el-input> -->
					<template>
						<el-select v-model="course.consultId" filterable placeholder="请选择">
							<el-option v-for="item in consultList" :key="item.consultId" :label="item.consultName"
								:value="item.consultId">
							</el-option>
						</el-select>
					</template>
				</el-form-item>
				<el-form-item label="课程简介" label-width="80px">
					<el-input placeholder="请输入课程简介" v-model="course.courseIntroduce"></el-input>
				</el-form-item>
				<el-form-item label="课程类型" label-width="80px">
					<template>
						<el-select v-model="course.courseTypeId" filterable placeholder="请选择">
							<el-option v-for="item in courseTypeList" :key="item.courseTypeId" :label="item.courseType"
								:value="item.courseTypeId">
							</el-option>
						</el-select>
					</template>
					<!-- <el-input placeholder="请输入课程类型" v-model="course.courseTypeId"></el-input> -->
				</el-form-item>
				<el-form-item label="课程时长" label-width="80px">
					<el-input placeholder="请输入课程时长" v-model="course.courseTime"></el-input>
				</el-form-item>
				<el-form-item label="课程节数" label-width="80px">
					<el-input placeholder="请输入课程节数" v-model="course.courseCount"></el-input>
				</el-form-item>
				<el-form-item label="适用人群" label-width="80px">
					<el-input placeholder="请输入适用人群" v-model="course.people"></el-input>
				</el-form-item>
				<el-form-item label="课程图片" label-width="80px">
					<!-- <el-input placeholder="请输入课程图片" v-model="article.articlePicture"></el-input> -->
					<!-- 
								 el-upload:上传组件
									action： 上传网址
									list-type：上传组件类型
									on-preview ： 图片预览事件
									on-remove ：移除图片事件
									el-dialog : 预览图片组件
									on-success : 上传成功后的回调函数
									limit  ： 限制上传图片个数
									fileList:就是用来展示图片的路径的数组
							-->
					<el-upload :action="url+'upload'" list-type="picture-card" :on-success="handleSuccess" :limit="1"
						:on-preview="handlePictureCardPreview" :on-remove="handleRemove" :file-list="fileList">
						<i class="el-icon-plus"></i>
					</el-upload>
					<el-dialog :visible.sync="dialogImgVisible">
						<img width="100%" :src="dialogImageUrl" alt="">
					</el-dialog>
				</el-form-item>
				
				<el-form-item label="视频资源" label-width="80px">
					<el-upload :action="url+'upload'"
						:show-file-list="false" :on-success="handleVideoOk" :before-upload="beforeVideoOk"
						>
						<video v-if="videoUrl != ''" :src="url+'download?filename='+videoUrl" style="width: 100%; height: 100%;" controls="controls"></video>
							<i class="el-icon-plus"></i>
					</el-upload>
				</el-form-item>
				

				<!-- 
								 el-upload:上传组件
									action： 上传网址
									list-type：上传组件类型
									on-preview ： 图片预览事件
									on-remove ：移除图片事件
									el-dialog : 预览图片组件
									on-success : 上传成功后的回调函数
									limit  ： 限制上传图片个数
									fileList:就是用来展示图片的路径的数组
							-->
				<!-- <el-form-item label="课程图片" label-width="80px">
					<el-upload :action="url+'upload'" list-type="picture-card" :on-success="handleSuccess" :limit="1"
						:on-preview="handlePictureCardPreview" :on-remove="handleRemove" :file-list="fileList">
						<i class="el-icon-plus"></i>
					</el-upload>
					<el-dialog :visible.sync="dialogVisible">
						<img width="100%" :src="dialogImageUrl" alt="">
					</el-dialog>
				</el-form-item> -->

			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click="cancel">取 消</el-button>
				<el-button type="primary" @click="addcourse">确 定</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	import {
		url
	} from '../../util/tool.js'
	export default {
		data() {
			return {

				url: '', // 定义一个全局的url
				// 查询信息对象
				queryInfo: {
					courseName: '',
					pageNum: 1,
					pageSize: 10
				},
				total: 0,
				courseList: [],
				title: '',
				dialogFormVisible: false,
				course: { // 课程对象
					collectionVolume: 0,
					consultId: '',
					courseCount: '',
					courseId: '',
					courseIntroduce: '',
					courseLearning: '',
					courseName: '',
					courseTime: '',
					courseTypeId: '',
					people: '',
					readingVolume: 0,
					reviewsCount: 0,
					subscription: '',
					videoPath: '',
					imgs:''
				},
				dialogImageUrl: '', // 预览图片路径
				dialogImgVisible: false, // 预览图片的对话框
				
				dialogVisible: false, // 预览视频的对话框
				fileList: [],
				courseTypes: [],
				courseTypeList: [],
				consults: [],
				consultList: [],
				// 视频路径
				videoUrl: ''
			}
		},
		methods: {
			// 处理上传成功的函数
			handleSuccess(response) {
				// 把上传后的图片路径
				this.course.imgs = response.message;
			},
			
			// 删除图片的事件
			handleRemove(file, fileList) {
				console.log(file, fileList);
				// 移除数据
				this.course.imgs = '';
			},
			
			// 预览图片事件
			handlePictureCardPreview(file) {
				this.dialogImageUrl = file.url;
				this.dialogImgVisible = true;
			},
			
			
			
			
			toSeeVideo(row){
				this.dialogVisible=true
				this.videoUrl = row.videoPath
				console.log(row.videoPath);
			},
			
			handleVideoOk(res,file) {
				this.course.videoPath = res.message;
				this.videoUrl = this.course.videoPath
			},
			beforeVideoOk(file) {
				if (["video/mp4", "video/ogg", "video/flv", "video/avi", "video/wmv", "video/rmvb", "video/mov"]
					.indexOf(file.type) == -1) {
					this.$message.error("请上传正确的视频格式");
					return false;
				} else {
					return true;
				}
			},

			// 查询所有课程分类信息
			// 查询所有的分类信息
			queryCourse() {
				this.$http.get('courseType/findAll').then(res => {
					this.courseTypeList = res.data.data
					for (var i = 0; i < res.data.data.length; i++) {
						this.$set(this.courseTypes, res.data.data[i].courseTypeId, res.data.data[i].courseType);
					}
				})
			},
			// 查询所有讲师的信息
			queryConsult() {
				this.$http.get('consult/findAllConsult').then(res => {
					this.consultList = res.data.data
					for (var i = 0; i < res.data.data.length; i++) {
						this.$set(this.consults, res.data.data[i].consultId, res.data.data[i].consultName);
					}
				})
			},
			
			cancel() {
				this.course = { // 课程对象
					collectionVolume: 0,
					consultId: '',
					courseCount: '',
					courseId: '',
					courseIntroduce: '',
					courseLearning: '',
					courseName: '',
					courseTime: '',
					courseTypeId: '',
					people: '',
					readingVolume: 0,
					reviewsCount: 0,
					subscription: '',
					videoPath: ''
				}
				// 关闭对话框
				this.dialogFormVisible = false;
				// 重新查询
				this.queryList()
				//  在取消函数，添加成功， 修改成功的函数中加入以下代码实现图片的清空
				this.fileList = []
				this.videoUrl=''
			},
			//	去删除课程内容
			toDeletecourse(courseId) {
				this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
						confirmButtonText: '确定',
						cancelButtonText: '取消',
						type: 'warning'
					})
					.then(() => {
						this.$http.delete('course/' + courseId)
							.then(res => {
								if (res.data.status == 200) {
									this.$message.success("删除成功")
									this.queryList()
								} else {
									this.$message.error("删除失败")
								}
							})
					})
					.catch(() => {
						this.$message({
							type: 'info',
							message: '已取消删除'
						})
					})
				//  在取消函数，添加成功， 修改成功的函数中加入以下代码实现图片的清空
				this.fileList = []
			},

			//	去修改课程内容
			toEditcourse(row) {
				console.log(row.imgs);
				// 赋值
				this.course = row;
				// 设置标题
				this.title = "修改课程";
				// 开启对话框
				this.dialogFormVisible = true;
				// 动态的获取图片的名称并帮到到fileList
				this.fileList = [{
					name: row.imgs,
					url: this.url + 'download?filename=' + row.imgs
				}];
				this.videoUrl=row.videoPath
			},

			//	添加或修改课程内容
			addcourse() {
				if (this.course.courseId) { //	修改	true
					this.$http.put("course", this.course)
						.then(res => {
							if (res.data.status == 200) {
								this.$message.success("修改成功！")
								// 清空数据
								this.course = { // 课程对象
									collectionVolume: 0,
									consultId: '',
									courseCount: '',
									courseId: '',
									courseIntroduce: '',
									courseLearning: '',
									courseName: '',
									courseTime: '',
									courseTypeId: '',
									people: '',
									readingVolume: 0,
									reviewsCount: 0,
									subscription: '',
									videoPath: ''
								}
								// 关闭对话框
								this.dialogFormVisible = false;
								// 重新查询
								this.queryList()
							} else {
								this.$message.error("修改失败！")
							}
						})
				} else { //	添加
					this.$http.post("course", this.course)
						.then(res => {
							if (res.data.status == 200) {
								this.$message.success("添加成功！")
								// 清空数据
								this.course = { // 课程对象
									collectionVolume: 0,
									consultId: '',
									courseCount: '',
									courseId: '',
									courseIntroduce: '',
									courseLearning: '',
									courseName: '',
									courseTime: '',
									courseTypeId: '',
									people: '',
									readingVolume: 0,
									reviewsCount: 0,
									subscription: '',
									videoPath: ''
								}
								// 关闭对话框
								this.dialogFormVisible = false;
								// 重新查询
								this.queryList()
							} else {
								this.$message.error("添加失败！")
							}
						})
				}
				//  在取消函数，添加成功， 修改成功的函数中加入以下代码实现图片的清空
				this.fileList = []
			},

			// 进入添加的对话框
			toAddcourse() {
				this.title = "新增课程";
				this.dialogFormVisible = true;

			},

			// 分页查询所有的数据
			queryList() {
				//	调用后端接口
				this.$http.get('course/findByPage', {
						params: this.queryInfo
					})
					.then(res => {
						if (res.data.status == 200) {
							console.log(res);
							this.courseList = res.data.data.list
							this.total = res.data.data.total
						} else {

						}
					})
			},
			handleSizeChange(size) {
				// console.log('每页显示数量发生改变');
				this.queryInfo.pageSize = size
				this.queryInfo.pageNum = 1
				this.queryList()
			},
			handleCurrentChange(num) {
				// console.log('页码发生改变');
				this.queryInfo.pageNum = num
				this.queryList()
			}
		},
		mounted() {
			this.queryList()
			this.queryCourse()
			this.queryConsult()
			// 动态的获取tool.js中的url，并赋值给当前组件的全局url
			this.url = url;
		}
	}
</script>
<style>
	/**设置富文本编辑器的最小高度，注意style中不能使用scoped **/
	.ql-container {
		min-height: 200px;
	}

	.el-breadcrumb {
		margin-bottom: 15px;
		padding: 0 0 15px;
		border-bottom: 1px solid #ddd;
	}

	.el-pagination {
		margin-top: 15px;
	}

	.el-link {
		padding: 0 5px;
	}
</style>